import EarlyWarning from "./earlyWarning"
import HashMap from "../uitls/hashMap"
import {get,post} from "@/ajax/http";
import "leaflet"
import '../uitls/leaflet-ext/proj4-compressed'
import '../uitls/leaflet-ext/proj4leaflet.js'
import {
    message,
} from 'antd';

const curWarning = function(viewer,config){
    this.viewer = viewer
    this.earlyWarning = new EarlyWarning(viewer)
    //一维预警对象列表
    this.warningObjs = []
    this.warningtwoObjs = []
    //一维预警指标对象Map
    this.indicatorObjs = new HashMap()
    //二维预警指标
    this.twoIndicator = null
    //一维计算结果对象
    this.resultList = []
    //二维计算结果对象
    this.tworesult = 0
    this.warningType = [
        "",
        "蓝色预警",
        "黄色预警",
        "橙色预警",
        "红色预警"
    ]
    this.isnotfinish = true
    var prjname = config.prjname || "EPSG:32648"
    var prjvalue = config.prjname || "+proj=utm +zone=48 +datum=WGS84 +units=m +no_defs"
    this.crs = new L.Proj.CRS(prjname,prjvalue,{})
}
curWarning.prototype = {
    changePrj:function(prj){
        if(prj){
            var prjname = prj.prjname || "EPSG:32648"
            var prjvalue = prj.prjvalue || "+proj=utm +zone=48 +datum=WGS84 +units=m +no_defs"
            this.crs = new L.Proj.CRS(prjname,prjvalue,{})
        }
    },
    //输入一维预警指标
    Inputindicators(riverNetworkId) {
        //清除之前的指标
        this.indicatorObjs.clear()
        
        var self = this
        self.isnotfinish = true
        get('/ews/bsEarlyWarningObject/gisList?riverNetworkId='+riverNetworkId+"&warningIndexCode=11").then((res) => {
             //插入新的指标
            res.data.forEach(element => {
                //获取断面中心点
                var sectionX = parseFloat((element.riverSection.leftX+element.riverSection.rightX)/2)
                var sectionY = parseFloat((element.riverSection.leftY+element.riverSection.rightY)/2)
                var sectionPoint = L.point(sectionX,sectionY);
                var latlng = self.crs.unproject(sectionPoint)
                self.indicatorObjs.put(element.riverSectionId,{
                    position:{
                        lon:latlng.lng,
                        lat:latlng.lat
                    },
                    info:{
                        name:element.riverSection.name  //断面名称
                    },
                    IndexList:element.earlyWarningIndexList //断面预警指标
                })
            });
        }).catch((err) => {
            console.log('查询一维预警指标失败')
            // message.error('查询预警指标失败')
        })
    },
    //输入二维预警指标
    InputTwoindicators(riverGridId) {
        //清除之前的指标
        this.indicatorObjs.clear()
        get('/ews/bsEarlyWarningObject/gisList?riverGridId='+riverGridId+"&warningIndexCode=21").then((res) => {
             //插入新的二维指标
             var position = {
                    lon:res.data[0].lon,
                    lat:res.data[0].lat
             }
             var info = {
                name:"未命名"  //断面名称
             }
             var  IndexList = res.data[0].earlyWarningIndexList //断面预警指标 
             this.twoIndicator = {
                position:position,
                info:info,
                IndexList:IndexList
             }
        }).catch((err) => {
            console.log('查询二维预警指标失败')
            // message.error('查询二维预警指标失败')
        })
    },
    //输入实时计算结果数据
    InputcurwarningObj(planId) {
        var self = this
        get('/ews/bsFloodControlSolution1dResult/solutionSectionMaxLevel/'+planId).then((res) => {
            //插入预警对象结果数据
            //判断返回值是否为空如果为空再请求一次
            if(res.data.length == 0 && self.isnotfinish){
                // setTimeout(function(){
                //     self.InputcurwarningObj(planId)
                // },2000)
            }else{
                this.resultList = []
                self.isnotfinish = true
                res.data.forEach(element => {
                this.resultList.push({
                    id:element.riverSectionId,
                    level:element.level
                })
               });
               this.checkWarning()
            }
           
       }).catch((err) => {
        //    message.error('查询计算统计结果失败')
       })
    },
    endInputcurwarningObj(){
        var self = this
        this.isnotfinish = false
        // setTimeout(function(){
            
        // },3000)
    },
    //输入二维实时计算结果数据
    InputTwocurwarningObj(planId) {
        get('/ews/bsFloodControlSolution/calcProcess2DTotalCellArea/'+planId).then((res) => {
            //
            this.tworesult = res.data
            this.checktwoWarning()
       }).catch((err) => {
        //    message.error('查询计算统计结果失败')
       })
    },
    //判断一维断面是否报警
    checkWarning() {
        this.warningObjs.length = 0
        var self = this
        if(this.resultList.length==0){
           
            return
        }
        if(!this.indicatorObjs){
            return
        }
        this.resultList.forEach(resultobj=>{
            //获取预警对象指标
            
            var warningobjTems = this.indicatorObjs.get(resultobj.id)
            if(warningobjTems && warningobjTems.IndexList){
                var warningLv = 0
                var IndexThresholdArr = []
                //对比实时水位和预警指标值
                warningobjTems.IndexList.forEach((warningTm,index) => {
                    var resLv = resultobj.level>warningTm.warningIndexThreshold?warningTm.warningLevel:warningLv
                    warningLv=warningLv>resLv?warningLv:resLv
                    IndexThresholdArr.push(warningTm.warningIndexThreshold)
                    if(index == warningobjTems.IndexList.length-1 && warningLv>0){
                        //将预警对象添加到预警显示队列
                        this.warningObjs.push({
                            position:{
                                lat:warningobjTems.position.lat,
                                lon:warningobjTems.position.lon
                            },
                            level:warningLv,
                            content:`<ul>
                                <li>断面名称:${warningobjTems.info.name}</li>
                                <li>预警阈值:${IndexThresholdArr.sort().join("|")}</li>
                                <li >当前水位:<span class=${"warinfo"+warningLv}>${resultobj.level}</span></li>
                                <li>预警级别:${this.warningType[warningLv]}</li>
                            </ul>`
                        })
                       
                    }
                });
            }
        })
        this.showWarning()
    },
    //判断二维网格是否报警
    checktwoWarning(){
        this.warningtwoObjs.length = 0
        var warningLv = 0
        var IndexThresholdArr = []
          //对比实时水位和预警指标值
        if(!this.twoIndicator.IndexList){
            // throw new Error("没有二维预警指标")
            return
        }
        this.twoIndicator.IndexList.forEach((warningTm,index) => {
            var resLv = this.tworesult>warningTm.warningIndexThreshold?warningTm.warningLevel:warningLv
            warningLv=warningLv>resLv?warningLv:resLv
            IndexThresholdArr.push(warningTm.warningIndexThreshold)
            if(index == this.twoIndicator.IndexList.length-1 && warningLv>0){
                //将预警对象添加到预警显示队列
                this.warningtwoObjs.push({
                    position:{
                        lat:this.twoIndicator.position.lat,
                        lon:this.twoIndicator.position.lon
                    },
                    level:warningLv,
                    content:`<ul>
                        <li>网格名称:${this.twoIndicator.info.name}</li>
                        <li>预警阈值:${IndexThresholdArr.join("|")}</li>
                        <li>淹没面积:${this.tworesult}km²</li>
                        <li>预警级别:${this.warningType[warningLv]}</li>
                    </ul>`
                })
              
            }
        });
        this.showWarning()
    },
    clearWarning(){
        this.warningObjs = []
        this.warningtwoObjs = []
        this.earlyWarning.clearAll()
    },
    //展示预警信息
    showWarning() {
        // debugger
        this.earlyWarning.clearAll()
        //在地图上展示预警信息
        this.earlyWarning.stationWarningToMap(this.warningObjs.concat(this.warningtwoObjs))
    }
}
export default curWarning